﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrbaBus.Properties;
using System.Data.SqlClient;

namespace FrbaBus.Devolucion
{
    public partial class FormDevolucion : Form
    {
        public FormDevolucion()
        {
            InitializeComponent();
            lCodigo.Visible = false;
            lSeleccion.Visible = false;
            lbItems.Visible = false;

        }

        private void btnDevolver_Click(object sender, EventArgs e)
        {


                string sConnectionString = Settings.Default.GD1C2013ConnectionString;
                SqlConnection connection = new SqlConnection(sConnectionString);
                SqlCommand cmd = new SqlCommand("THE_BEST_IAS.pr_devolver", connection);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add(new SqlParameter("@p_dev_fecha", SqlDbType.DateTime)).Value = Fechas.fechaSistema;
                cmd.Parameters.Add(new SqlParameter("@p_comp_codigo", SqlDbType.BigInt)).Value = Convert.ToInt64(tbVoucher.Text.Trim().ToString());
                cmd.Parameters.Add(new SqlParameter("@p_dev_motivo", SqlDbType.VarChar)).Value = tbMotivo.Text.Trim().ToString();

                cmd.Parameters.Add("@v_dev_codigo", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                cmd.CommandTimeout = 1000;

                connection.Open();
                cmd.ExecuteNonQuery();

                long codigoDevolucion = Convert.ToInt64(cmd.Parameters["@v_dev_codigo"].Value);

                connection.Close();

                foreach (Object selecteditem in lbItems.SelectedItems)
                {
                    sConnectionString = Settings.Default.GD1C2013ConnectionString;
                    connection = new SqlConnection(sConnectionString);
                    cmd = new SqlCommand("THE_BEST_IAS.pr_devolver_item", connection);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add(new SqlParameter("@p_coit_codigo", SqlDbType.BigInt)).Value = Convert.ToInt64(selecteditem.ToString());
                    cmd.Parameters.Add(new SqlParameter("@p_dev_codigo", SqlDbType.BigInt)).Value = codigoDevolucion;

                    //cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                    cmd.CommandTimeout = 1000;

                    connection.Open();
                    cmd.ExecuteNonQuery();
                    connection.Close();
                    //if (valorRetorno == 0)
                    //{
                    //    MessageBox.Show("Se realizó la devolución del item " + item + " correctamente", "INFORMACION");
                    //}
                    //if (valorRetorno == 1)
                    //{
                    //    MessageBox.Show("No se pudo realizar la devolución del item " + item, "ERROR");
                    //}
                    this.Hide();
                }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void btnVerItems_Click(object sender, EventArgs e)
        {

            string query = "select coit_codigo from THE_BEST_IAS.compra_items where comp_codigo = " + tbVoucher.Text.Trim().ToString();
                
            SqlDataReader dr = DbHelper.ExecuteQuery(query);
           
            if (dr.HasRows)
            {
                int i = 0;
                while (dr.Read())
                {
                    lbItems.Items.Insert(i, dr[0].ToString());
                    i++;
                }
            }
            

            lCodigo.Visible = true;
            lSeleccion.Visible = true;
            lbItems.Visible = true;
        }
    }
}
